package 算法.LeetCode91.每日打卡题目;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 谢君臣
 * @create: 2021-02-02 20:00
 * @founction: XXXX
 * 请你设计一个支持下述操作的栈。
 * <p>
 * 实现自定义栈类 CustomStack ：
 * <p>
 * CustomStack(int maxSize)：用 maxSize 初始化对象，maxSize 是栈中最多能容纳的元素数量，栈在增长到 maxSize 之后则不支持 push 操作。
 * void push(int x)：如果栈还未增长到 maxSize ，就将 x 添加到栈顶。
 * int pop()：弹出栈顶元素，并返回栈顶的值，或栈为空时返回 -1 。
 * void inc(int k, int val)：栈底的 k 个元素的值都增加 val 。如果栈中元素总数小于 k ，则栈中的所有元素都增加 val 。
 * <p>
 * 示例：
 * <p>
 * 输入：
 * ["CustomStack","push","push","pop","push","push","push","increment","increment","pop","pop","pop","pop"]
 * [[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]]
 * 输出：
 * [null,null,null,2,null,null,null,null,null,103,202,201,-1]
 */
public class day03 {
    public static void main(String[] args) {
        String[] key = {"CustomStack", "push", "push", "pop", "push", "push", "push", "increment", "increment", "pop", "pop", "pop", "pop" };
        int[][] input = {{3}, {1}, {2}, {}, {2}, {3}, {4}, {5, 100}, {2, 100}, {}, {}, {}};
        List<Integer> list = Solution(key, input);
        System.out.println(list);
    }

    private static List<Integer> Solution(String[] key, int[][] input) {
        List<Integer> list = new ArrayList<>();
        CustomStack customStack = null;
        int i = 0;
        while (i < key.length) {
            if (key[i].equals("CustomStack")) {
                customStack = new CustomStack(input[i][0]);
                list.add(null);
            } else if (key[i].equals("push")) {
                int k = input[i][0];
                customStack.push(input[i][0]);
                list.add(null);
            } else if (key[i].equals("pop")) {
                list.add(customStack.pop());
            } else if (key[i].equals("increment")) {
                customStack.increment(input[i][0],input[i][1]);
                list.add(null);
            }
            i++;
        }
        return list;
    }
}

class CustomStack {
    int[] array;
    int MaxSize;
    int size;

    public CustomStack(int maxSize) {
        size = -1;
        MaxSize = maxSize;
        array = new int[maxSize];
    }

    public void push(int x) {
        if (size + 1 < MaxSize)
            array[++size] = x;
    }

    public int pop() {
        if (size == -1)
            return -1;
        else
            return array[size--];
    }

    public void increment(int k, int val) {
        for (int i = 0; i <= size; i++) {
            if (i == k)
                break;
            array[i] += val;
        }
    }

}